package com.comac.print.component.app.handler;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.comac.print.component.app.gatewayimpl.database.dataobject.MqLogDO;
import com.comac.print.component.app.gatewayimpl.database.mapper.MqLogMapper;
import com.comac.print.component.app.utils.ToStringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Description MQ日志处理类
 * @Author YY
 * @Date 2022/4/19 17:29
 */
@Component
public class MQLogHandler {

    @Autowired
    private MqLogMapper mqLogMapper;
    
    @Async
    public void addMqLog(Message message, Object coreParam, Object result, Exception exception){
        MqLogDO mqLogDO = MqLogDO.builder()
                .msgId(message.getMsgID())
                .topic(message.getTopic())
                .tag(message.getTag())
                .coreParam(JSONObject.toJSONString(coreParam))
                .result(JSONObject.toJSONString(result))
                .logType("error")
                .createTime(LocalDateTime.now())
                .exception(ToStringUtil.getExceptionToString(exception))
                .build();
        mqLogMapper.insert(mqLogDO);
    }

    @Async
    public void addMqLog(Message message, Object coreParam, Object result){
        MqLogDO mqLogDO = MqLogDO.builder()
                .msgId(message.getMsgID())
                .topic(message.getTopic())
                .tag(message.getTag())
                .coreParam(JSONObject.toJSONString(coreParam))
                .result(JSONObject.toJSONString(result))
                .logType("normal")
                .createTime(LocalDateTime.now())
                .build();
        mqLogMapper.insert(mqLogDO);
    }
}
